package ApplicationsOld;

import java.io.IOException;


import AdminController.AdminExe.*;
import ApplicationsYoung.ApplicationsYoungBean;
import MassageController.Massage.MassageBean;
import MassageController.Massage.MassageDAO;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;

import com.oreilly.servlet.*;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ApplicationsOld extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
    public ApplicationsOld() {
        super();
       
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("*****위탁신청 서블릿*****");
		
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		RequestDispatcher view = null;
		PrintWriter out =response.getWriter();
		
		String action_code = request.getParameter("action_code");
		ApplicationsOldBean oldBean = new ApplicationsOldBean();
		ApplicationsOldDAO  oldDao  = new ApplicationsOldDAO();
		
		
		
		AdminExeDAO aDao = new AdminExeDAO();
		AdminExeBean aeyBean =  new AdminExeBean();
		
		//AdminExeBean[] Vcheck = null;
		//AdminExeBean[] Dcheck = null;
		ApplicationsYoungBean aYbean[] = null;
		AdminExeBean[] aeBeanB = null;
		
		ArrayList<String> Vvaluelist ;
	
		if(action_code.equals("Appli")){
			String numMode = request.getParameter("num");
			int start = numMode != null ? Integer.parseInt(numMode) : -1;
			
				switch(start){
					case 1:
						
						String Mem_Nick = request.getParameter("Mem_Nick");
						int idnumber = oldDao.mIdCheck(Mem_Nick); // 1차맵핑된 노인측 숫자아이디
						int check = oldDao.babyPlusInquiry(idnumber); // 노인측 상태채크
						int youngNum = oldDao.choiceParent(idnumber); // 노인과 1차맵핑된 부모 숫자아이디
						
						aeyBean.setMem_Id(youngNum);
						aYbean = oldDao.getParent(youngNum); //부모정보 획득 
						String Vcheck = aDao.getVvalueYList(aeyBean); //부모 자격증획득
						String Dcheck = aDao.getDvalueYList(aeyBean); //부모 날짜획득
						aeBeanB = oldDao.getChildphoto(aeyBean); //자식사진 획득
						
						
						if(check == 1 || check == 3 || check == 4 || check == 5){
								request.setAttribute("list",aYbean);
								request.setAttribute("Vlist",Vcheck);
								request.setAttribute("Dlist",Dcheck);
								request.setAttribute("child",aeBeanB);
								request.setAttribute("page","/Applications/Old/Fm_MassageView");
								request.setAttribute("check",check);
								view=request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");
				 				view.forward(request,response);
				 				break;
						}else if(check == 0){
								request.setAttribute("page","/Applications/Old/Fm_AppOld");
								view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");
								view.forward(request, response);
								break;
						}
					case 2:
						
						System.out.println("일단환영");
						String realFolder = "";

						String saveFolder = getServletContext().getRealPath("Applications/Old/photo");
						String encType = "UTF-8";
						int maxSize = 1*1024*1024;
						String filename = "";
						

						MultipartRequest multi = null;
						
						multi = new MultipartRequest( request, saveFolder, maxSize, encType,
								new DefaultFileRenamePolicy() );
						
						Enumeration files = multi.getFileNames();
						
						String file = ( String )files.nextElement();
						filename = multi.getFilesystemName( file );
						
						
						String Vvalue[] = multi.getParameterValues("Vvalue");
						String Dvalue[] = multi.getParameterValues("Dvalue");
						
						
						
						System.out.println("위탁 성별:"+multi.getParameter("Old_Gender"));
						System.out.println("위탁종류:"+multi.getParameter("Old_Kind"));
						for(int i = 0 ; i <Vvalue.length ; i++){
							System.out.println("위탁자격:"+Vvalue[i]);
						}
						for(int i = 0 ; i <Dvalue.length ; i++){
							System.out.println("위탁일정:"+Dvalue[i]);
						}
						
						System.out.println("위탁시급:"+multi.getParameter("Old_Money"));
						System.out.println("위탁사진:"+filename);
						System.out.println("해당아이디:"+multi.getParameter("Mem_Nick"));
						
						
						oldBean.setOld_Gender(Integer.parseInt(multi.getParameter("Old_Gender")));
						oldBean.setOld_Kind(multi.getParameter("Old_Kind"));
						oldBean.setVvalue(multi.getParameterValues("Vvalue"));
						oldBean.setDvalue(multi.getParameterValues("Dvalue"));
						oldBean.setOld_Money(multi.getParameter("Old_Money"));
						oldBean.setOld_Photo(multi.getOriginalFileName("Old_Photo"));
						
						int id = oldDao.mIdCheck(multi.getParameter("Mem_Nick"));
						
						String address = oldDao.cityCheck(id); //해당거주지
						oldDao.oldCheck(oldBean,id, address);//수인자 정보등록
						int sum = oldDao.abliltyCheck(oldBean,id);//수인자 자격증 등록
						int Dsum = oldDao.dayCheck(oldBean, id);//수인자 날짜등록
						oldDao.abliltySum(sum, id);//자격증 총합 등록
						oldDao.dayCheckSum(Dsum, id);//날짜 총합 등록
						oldDao.adminOld(multi.getParameter("Mem_Nick"), id);//관리자 수인등록
						
						
						System.out.println("일단 저장 완료!!");
					
						out.println( "<script>" );
						out.println( "alert(\"受け付けられました！\");" );
						out.println( "location.href=\"MemberLogAction.do?action_code=sessioncheck&Mem_Nick="+multi.getParameter("Mem_Nick")+"&result=1\";" );
						out.println( "</script>" );
						break;
				}
		}else if(action_code.equals("delet")){
			
			String id = request.getParameter("id");
			
			System.out.println("접수신청취소");
			int idnumber = oldDao.mIdCheck(id);
			oldDao.cancelSet(idnumber);

			System.out.println("id넘버 = " + idnumber);
			out.println( "<script>" );
			out.println( "location.href=\"/KOMORIUTA/MOldAction.do?\";" );
			out.println( "</script>" );
		
	}else if(action_code.equals("success")){
		
		System.out.println("최종 승락 완료");
		
		int check = Integer.parseInt(request.getParameter("check"));
	
		if(check == 1){
			
			int up=5;
			int mFup = 1;
			String id = request.getParameter("id");
			
			int idnumber = oldDao.mIdCheck(id);
			
			oldDao.finalacept(idnumber, up);
			int young=oldDao.mapYoung(idnumber);
			oldDao.finalaceptY(young, up);
			oldDao.finalkotoY(young, up, mFup);
			
			oldDao.finalDeathO(idnumber);
			
			MassageDAO massageDao = new MassageDAO();
			MassageBean massageDeep = new MassageBean();
				
			massageDeep.setMsgTo(aDao.checkNick(young));
			massageDeep.setMsgForm("管理者");
			massageDeep.setMsgContents(id+"様と繋がれました。求人申請で承認をください！");
			massageDao.Write(massageDeep);
			
			out.println( "<script>" );
			out.println( "location.href=\"/KOMORIUTA/MOldAction.do?\";" );
			out.println( "</script>" );
		
		}else if(check == 2){
			
			int zup=5;
			int zmFup = 1;
			String id = request.getParameter("id");
			int idnumber = oldDao.mIdCheck(id);
			System.out.println("관리자 개색키한테 온 번호:"+id);
			System.out.println("부모번호:"+idnumber);
			int idOnumber = oldDao.mapOld(idnumber);
			oldDao.finalacept(idOnumber, zup);
			int young=oldDao.mapYoung(idOnumber);
		
			oldDao.finalaceptY(young, zup);
			oldDao.finalkotoY(young, zup, zmFup);
			oldDao.finalDeathO(idOnumber);
		
			out.println( "<script>" );
			out.println( "location.href=\"/KOMORIUTA/AdminExe.do?action_code=FinalAcept&num=1\";" );
			out.println( "</script>" );
		}
		
	}else if(action_code.equals("disable")){
		System.out.println("승락 거절");
		
		int check = Integer.parseInt(request.getParameter("check"));
		
		if(check == 1){
			
			int up=3;
			int Fup=1;
			int mFup = 3;
			
			String id = request.getParameter("id");
			int idnumber = oldDao.mIdCheck(id);
			int young=oldDao.mapYoung(idnumber);
			
			oldDao.finalacept(idnumber, up);
			oldDao.finalkotoO(idnumber, Fup);
			oldDao.finalkotoY(young, Fup , mFup);
			
			out.println( "<script>" );
			out.println( "location.href=\"/KOMORIUTA/MOldAction.do?\";" );
			out.println( "</script>" );
			
		}else if(check == 2){

			int zup=3;
			int zFup=1;
			int zmFup = 3;
			
			String id = request.getParameter("id");
			int idnumber = oldDao.mIdCheck(id); //해당 부모닉의 숫자아이디 추출
			int old = oldDao.mapOld(idnumber); //해당 부부맵핑된 할아버지 숫자아이디 추출
			
			oldDao.finalacept(old, zup);
			oldDao.finalkotoO(old, zFup);
			oldDao.finalkotoY(idnumber, zFup , zmFup);
			
			out.println( "<script>" );
			out.println( "location.href=\"AdminExe.do?action_code=FinalAcept&num=1\";" );
			out.println( "</script>" );
		}
		
	
	}else if(action_code.equals("mapfinal")){
		System.out.println("오프라인 최종승락");
		
		int mid = Integer.parseInt(request.getParameter("mid"));
		System.out.println(mid);
		int fcheck = Integer.parseInt(request.getParameter("fcheck"));
		
		System.out.println("최종부모"+mid);
		System.out.println("최종승낙"+fcheck);
		
		if(fcheck == 2){
			System.out.println("최종 승락 완료 애 잘키우세요!!");
			int check = 2;
			oldDao.finalMappingY(check, mid);
			oldDao.MappingDieY(mid);
			
			out.println( "<script>" );
			out.println( "location.href=\"AdminExe.do?action_code=FinalAcept&num=1\";" );
			out.println( "</script>" );	
		
		}else if(fcheck == 1){
			System.out.println("니가 싫다 죽도록 싫다");
			int up = 1;
			int mFup = 3;
			
			int oldid = oldDao.mapOld(mid);
			System.out.println("상대방 할배:"+oldid);
			oldDao.finalkotoY(mid, up, mFup); //오프라인실패로 인한 강등
			oldDao.dieMappingY(mid); //맵핑테이블에서 삭제..
			oldDao.finalaceptY(mid, mFup); // 부모테이블 레벨 강등..
			oldDao.finalacept(oldid,up); //할아버지 다시 검색될수 있게..
			
			out.println( "<script>" );
			out.println( "location.href=\"AdminExe.do?action_code=FinalAcept&num=1\";" );
			out.println( "</script>" );	
		}
	}
}
}
